<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>单选题答题页面</title>
<meta name="renderer" content="webkit">
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
<style>
body,.main-body{
    overflow: hidden;
}
.main-body {
    font-family: Arial, sans-serif;
    max-width: 88%;
    margin: auto;
    padding: 20px;
    /* font-size: 36px; */
}
#quizForm p{
    align-items: center;
    border: 1px solid #e0e2eb;
    border-radius: 12px;
    cursor: pointer;
    min-height: 20px;
    justify-content: space-between;
    padding: 16px 16px;
    position: relative;
}
#quizForm p:hover{    
    background: #eae8fd;
    border-color: #615ced;
}
.select{
    border-color: #4CAF50!important;
    background: #d0fed1!important;
}

#quizForm>p>span{
    padding: 10px;
}

.question-container {
    border: 1px solid #ccc;
    padding: 20px;
    border-radius: 5px;
    display: none;
}

button {
    background-color: #4CAF50;
    color: white;
    padding: 10px 20px;
    border: none;
    cursor: pointer;
    text-align: center;
    text-decoration: none;
    display: inline-block;
    font-size: 16px;
    margin: 10px 2px;
    cursor: pointer;
    border-radius: 5px;
}

button:hover {
    background-color: #45a049;
}
#question-title{
    font-weight: bold;
    font-size: 2em;
    margin-top: 10px;
    margin-bottom: 10px;
}
#question-title span{
    background-color: #e6e6e9;
    border-radius: 60px;
    padding: 4px;
    color: #fff;
    margin: 12px;
    margin-right: 10px;
    font-size: 30px;
}
.tips {
    padding: 20px;
    z-index: 5;
    position: absolute;
    bottom: 0;
    margin-top: 10px;
    margin-left: 10px;
    width: 80%;
    color: #ccc;
}
/* 滚动条样式 */
::-webkit-scrollbar {
    /*滚动条整体样式*/
    width : 2px;  /*高宽分别对应横竖滚动条的尺寸*/
    height: 1px;
}
::-webkit-scrollbar-thumb {
    /*滚动条里面小方块*/
    border-radius: 3px;
    box-shadow   : inset 0 0 5px rgba(0, 0, 0, 0.2);
    background   : #aaf0d1;
}
::-webkit-scrollbar-track {
    /*滚动条里面轨道*/
    box-shadow   : inset 0 0 5px rgba(0, 0, 0, 0.2);
    border-radius: 3px;
    background   : #ededed;
}
/* PC网页的样式 */
@media screen and (min-width: 450px) {
    .main-body{
        font-size: 36px;
    }
    #question-title span {
        padding: 12px;
        font-size: 50px;
    }
}
.rabbit {
    position: fixed;
    right: 30px;
    bottom: 50px;
    z-index: 50;
    display: none;
}
.rabbit img {
    height: 100px;
}
</style>
<link href="https://mirror.fe80.cn/limonte-sweetalert2/11.11.0/sweetalert2.min.css" rel="stylesheet">
<link href="https://mirror.fe80.cn/animate.css/4.1.1/animate.min.css" rel="stylesheet">
</head>
<body>
<div class="main-body">
    <div class="question-container">
        <p id="question-title"><span>0</span></p>
        <div id="quizForm">
            <p><span>A.</span>-</p>
            <p><span>B.</span>-</p>
            <p><span>C.</span>-</p>
            <p><span>D.</span>-</p>
            <!-- <button type="button" id="checkAnswer">下一题</button> -->
        </div>
        <p id="result"></p>
    </div>
</div>
<div class="tips"></div>
<div class="rabbit">
    <img src="http://hello.fe80.cn/static/images/rabbit.gif" alt="兔子">
</div>
<script src="https://mirror.fe80.cn/jquery/3.6.1/jquery.min.js"></script>
<script src="https://mirror.fe80.cn/limonte-sweetalert2/11.11.0/sweetalert2.min.js"></script>
<script>
const now = new Date();
var formattedDate = `${now.getFullYear()}${String(now.getMonth() + 1).padStart(2, '0')}${String(now.getDate()).padStart(2, '0')}${String(now.getHours()).padStart(2, '0')}${String(now.getMinutes()).padStart(2, '0')}${String(now.getSeconds()).padStart(2, '0')}`;
console.log("========"+formattedDate+"========");
var current_option = -1;//当前选项
var old_option = -2; //上一个选项
var current_question = {};//当前题目
var submit_question = 0;//当前题目ID
var global = {fullscreen:0,difficulty:0};
var question_cnt = {"success":0,"error":0};
$(function(){
    var resultElement = document.getElementById("result");
    // 获取题目
    function GetProblems(){
        current_option = -1;
        old_option = -2;
        var datas = {notid:current_question.question_id,course_id:1,difficulty:global.difficulty,total:(question_cnt.success),requestid:formattedDate};
        $.post('https://i.fe80.cn/portal/dun/choice',datas,function(res){
            if(res.status == 1){
                current_question = res.item;
                $("#question-title").html("<span>"+res.item.question_id+"</span>"+res.item.question_text);
                $("#quizForm").children('p').eq(0).html("<span>A.</span>"+res.item.option_a);
                $("#quizForm").children('p').eq(1).html("<span>B.</span>"+res.item.option_b);
                $("#quizForm").children('p').eq(2).html("<span>C.</span>"+res.item.option_c);
                $("#quizForm").children('p').eq(3).html("<span>D.</span>"+res.item.option_d);
                $(".question-container").show();
                $("#quizForm").children('p').removeClass("select");
                resultElement.textContent = "";
                current_question_id = res.item.question_id;
            }else{
                Swal.fire({
                    position: 'center', //定位 左上角
                    icon: 'warning',
                    title: res.msg,
                    showConfirmButton: false,
                    timer: 1500
                });
            }
        });
    }
    // 检查回答的是否正确
    function checkAnswer(){
        // console.log(current_option);
        if(current_option == old_option){
            GetProblems();
        }else if(current_option >= 0){
            if(current_option == current_question.correct_answer - 1){
                Swal.fire({
                    position: 'center', //定位 左上角
                    icon: 'success',
                    title: '回答正确！',
                    showConfirmButton: false,
                    timer: 1500
                });
                resultElement.textContent = "恭喜你，回答正确！";
                resultElement.style.color = "green";
                if(current_question.question_id != submit_question){
                    question_cnt.success += 1;
                }
                submit_question = current_question.question_id;
                if(Math.random() > 0.8){
                    $(".rabbit").css("display","block");
                    setTimeout(function(){$(".rabbit").fadeOut();},2500);
                }
                setTimeout(function(){GetProblems();},1900);//1.9秒后自动更新题目
            }else{
                let keys = ['A','B','C','D'];
                // 根据后台设置是否要展示正确答案
                resultElement.textContent = "很遗憾，回答错误！"
                +(current_question.showk == 1 ? "正确答案是："+keys[current_question.correct_answer-1]: "");
                resultElement.style.color = "red";
                Swal.fire({
                    position: 'center', //定位 左上角
                    icon: 'error',
                    title: '回答错误！',
                    showConfirmButton: false,
                    timer: 1500
                });
                old_option = current_option;
                if(current_question.question_id != submit_question){
                    question_cnt.error += 1;
                }
                submit_question = current_question.question_id;
            }
            $(".tips").text("对:"+question_cnt.success+",错:"+question_cnt.error);
            // 如果题目有解释，把解释显示出来
            if(current_question.showk == 1 && current_question.explain)
            resultElement.innerHTML = resultElement.textContent + "<br>" + current_question.explain;
            // current_option = 4; //标记需要换题
        }else{
            Swal.fire({
                toast: true,
                position: 'top',
                showConfirmButton: false,
                timer: 3000,
                icon: 'warning',
                title: '请先选择选项！'
            });
            // resultElement.textContent = "请先选择选项！";
            resultElement.style.color = "#00F";
        }
    }
    GetProblems();
    $("#checkAnswer").on('click',function(){
        checkAnswer();
    });
    $("#quizForm").on('click','p',function(){
        $(this).siblings().removeClass('select');
        $(this).addClass('select');
        current_option = $(this).index();
        checkAnswer();
    });
    // 切换选项
    function SwitchOption(num){
        current_option = current_option + num;
        if(current_option >= 4){
            current_option = 0;
        }else if(current_option < 0){
            current_option = 3;
        }
        $("#quizForm").children('p').removeClass("select");
        $("#quizForm").children('p').eq(current_option).addClass("select");
    }
    // 检测翻页笔
    document.onkeydown = function(e){
        console.log(e.keyCode);
        // 按Enter键或者翻页笔的上键提交
        if(e.keyCode == 9){
            checkAnswer();
        }else if(e.keyCode == 13){
            // 按6次退出全屏
            if(global.fullscreen == 1){
                global.fullscreen = 0;
                if(document.exitFullScreen) {
                    document.exitFullScreen();
                } else if(document.mozCancelFullScreen) {
                    document.mozCancelFullScreen();
                } else if(document.webkitExitFullscreen) {
                    document.webkitExitFullscreen();
                } else if(document.msExitFullscreen) {
                    document.msExitFullscreen();
                }
            }else if(global.fullscreen == 0){
                global.fullscreen = 1;
                // 按3次全屏模式
                var elem = document.documentElement;
                if (elem.requestFullscreen) {
                    elem.requestFullscreen();
                } else if (elem.webkitRequestFullscreen) {
                    elem.webkitRequestFullscreen();
                } else if (elem.msRequestFullscreen) {
                    elem.msRequestFullscreen();
                }
            }
        }else if(e.keyCode == 40 || e.keyCode == 34){
            // 翻页笔的下键切换选项
            SwitchOption(1);
        }else if(e.keyCode == 38 || e.keyCode == 33){
            SwitchOption(-1);
        }else if(e.keyCode == 66){
            // 长按下键
            global.difficulty += 1;
            if(global.difficulty > 3){
                global.difficulty = 0;
            }
            Swal.fire({
                toast: true,
                position: 'top-start',
                showConfirmButton: false,
                timer: 3000,
                icon: 'warning',
                title: '题目难度已切换为：' + ["全部","简单","中等","较难"][global.difficulty]
            });
        }else if(e.keyCode == 27){
            // 长按上键
            window.location.href = window.location.href;
        }
        if([9,13,33,34,38,40,66,27].includes(e.keyCode)){
            console.log("阻止提交");
            return false;
        }
    }
    $(".tips").on('click',function(){
        question_cnt = {"success":0,"error":0};
        $(".tips").text("对:"+question_cnt.success+",错:"+question_cnt.error);
    });
});
</script>
</body>
</html>